﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace AB_Adb_Tools
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void ExecuteAdbCommand(string command, bool showWindow = false)
        {
            try
            {
                ProcessStartInfo psi = new ProcessStartInfo
                {
                    FileName = "cmd.exe",
                    Arguments = $"/C {command}",
                    RedirectStandardOutput = !showWindow,
                    RedirectStandardError = !showWindow,
                    UseShellExecute = showWindow,
                    CreateNoWindow = !showWindow
                };

                Process process = new Process { StartInfo = psi };
                process.Start();

                if (!showWindow)
                {
                    string output = process.StandardOutput.ReadToEnd();
                    string error = process.StandardError.ReadToEnd();
                    process.WaitForExit();

                    txtOutput.AppendText($"> {command}\r\n");
                    if (!string.IsNullOrEmpty(output))
                        txtOutput.AppendText($"{output}\r\n");
                    if (!string.IsNullOrEmpty(error))
                        txtOutput.AppendText($"错误: {error}\r\n");
                    txtOutput.AppendText("\r\n");
                }
                else
                {
                    process.WaitForExit();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"执行命令时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnAdbDriver_Click(object sender, EventArgs e)
        {
            Process.Start("https://zhuanlan.zhihu.com/p/653371171");
        }

        private void btnPair_Click(object sender, EventArgs e)
        {
            string scriptPath = Path.Combine(Application.StartupPath, "script", "adbpair.cmd");

            if (File.Exists(scriptPath))
            {
                try
                {
                    // 直接执行adbpair.cmd文件
                    ProcessStartInfo psi = new ProcessStartInfo
                    {
                        FileName = scriptPath,
                        UseShellExecute = false,
                        CreateNoWindow = false  // 这里设置为false会显示cmd窗口
                    };

                    Process.Start(psi);
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"执行adbpair.cmd时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show($"找不到adbpair.cmd文件，请确保script目录存在且包含该文件。\n路径: {scriptPath}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 基本控制
        private void btnBack_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell input keyevent 4");
        private void btnHome_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell input keyevent 3");
        private void btnPower_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell input keyevent 26");
        private void btnMenu_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell input keyevent 82");

        // 音量控制
        private void btnVolumeUp_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell input keyevent 24");
        private void btnVolumeDown_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell input keyevent 25");

        // 媒体控制
        private void btnNext_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell input keyevent 87");
        private void btnPrev_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell input keyevent 88");
        private void btnStop_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell input keyevent 86");
        private void btnPlay_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell input keyevent 126");
        private void btnPause_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell input keyevent 127");
        private void btnMute_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell input keyevent 164");

        // 电源控制
        private void btnScreenOn_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell input keyevent 224");
        private void btnScreenOff_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell input keyevent 223");
        private void btnUnlock_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell input swipe 300 1000 300 500");
        private void btnReboot_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb reboot");

        // 设备信息 (修改为不显示cmd窗口，结果直接显示在文本框中)
        private void btnGetBrand_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell getprop ro.product.brand");
        private void btnGetBoard_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell getprop ro.product.board");
        private void btnGetDeviceModel_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell getprop ro.product.model");
        private void btnGetAndroidVersion_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell getprop ro.build.version.release");
        private void btnGetNetworkType_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell getprop gsm.network.type");
        private void btnGetOperator_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell getprop gsm.operator.alpha");
        private void btnGetCPUArch_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell getprop ro.product.cpu.abi");
        private void btnGetDevices_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb devices");
        private void btnGetRenderer_Click(object sender, EventArgs e) => ExecuteAdbCommand("adb shell dumpsys SurfaceFlinger | findstr \"GLES\"");
    }
}